﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using Game.Global.Helper;

using Newtonsoft.Json;

namespace Game
{
	public class MissionProvider  
	{
        /// <summary>
        /// 配置文件的目录名
        /// </summary>
        const string C_MissionConfigPathName = "MissionConfig";
        
        //---------------------------------------------------------------------------
		//				Public area
		//---------------------------------------------------------------------------
		#region Public area

        /// <summary>
        /// 获得第N等级下的预制任务
        /// </summary>
        /// <remarks>读取配置文件</remarks>
        public static GameMission ReadMission(int missionIndex)
        {
            //读出该等级下的配置
            string configContent = FileSaver.LoadResource(FileSaver.PathType.GameData, C_MissionConfigPathName, missionIndex.ToString());

            GameMission mission = JsonHelper.Deserialize<GameMission>(configContent);
            
            return mission;
        }

        /// <summary>
        /// 保存为第N等级下的任务（仅用于编辑器）
        /// </summary>
        /// <returns>是否保存成功</returns>
        public static bool SaveMission(GameMission mission, int missionIndex)
        {
            //先序列化为string，再保存
            string configContent = JsonHelper.Serialize(mission);
            FileSaver.SaveResource(missionIndex.ToString(), configContent, FileSaver.PathType.GameData, C_MissionConfigPathName);

            return true;
        }

        /// <summary>
        /// 获得某个种子下随机的预制细胞集
        /// </summary>
        public List<IntVector2> GetRandomCells(int maxPos, int seed)
        {
            List<IntVector2> resultSet = new List<IntVector2>();
            //Generate a random map
            Random.seed = seed;

            IntVector2 randPos = new IntVector2();
            int randCount = maxPos * maxPos / 2;
            for (int i = 0; i < randCount; i++)
            {
                randPos.x = Random.Range(0, maxPos);
                randPos.y = Random.Range(0, maxPos);

                resultSet.Add(randPos);
            }

            return resultSet;

        }
		#endregion

		//---------------------------------------------------------------------------
		//				Private area
		//---------------------------------------------------------------------------
		#region Private area
		#endregion
	}
}